﻿Public Class ViewTableViewer

    Private Sub BtTrier_Click(sender As Object, e As Windows.RoutedEventArgs)
        SortGrid.Visibility = Windows.Visibility.Visible

        Dim vm As ViewModelTableViewer = DataContext

        Dim cview As New Windows.Data.CollectionViewSource()

        cview.SortDescriptions.Add(New ComponentModel.SortDescription("Priority", ComponentModel.ListSortDirection.Ascending))

        cview.Source = (From c As DataColumn In vm.DataSource.Columns
                        Select New SortInfo(c.ColumnName, False, False, c.Ordinal)).ToList()

        'SortLst.ItemsSource = cview
        Windows.Data.BindingOperations.SetBinding(SortLst, Windows.Controls.ListBox.ItemsSourceProperty, New Windows.Data.Binding() With {.Source = cview})
    End Sub

    Private Sub SortOkBt_Click(sender As Object, e As Windows.RoutedEventArgs)
        SortGrid.Visibility = Windows.Visibility.Collapsed

        Dim vm As ViewModelTableViewer = DataContext

        Dim q = From elt As SortInfo In SortLst.ItemsSource
                Where CBool(elt.Sort)
                Order By CInt(elt.Priority)
                Select CStr(elt.Name) & If(CBool(elt.Descending), " DESC", "")

        vm.DataView.Sort = String.Join(",", q.ToArray())
    End Sub

    Private Sub SortKoBt_Click(sender As Object, e As Windows.RoutedEventArgs)
        SortGrid.Visibility = Windows.Visibility.Collapsed

    End Sub

    Public Class SortInfo

        Sub New(Name As String, Sort As Boolean, Descending As Boolean, Priority As Integer)
            ' TODO: Complete member initialization 
            Me.Name = Name
            Me.Sort = Sort
            Me.Descending = Descending
            Me.Priority = Priority
        End Sub

        Public Property Name As String
        Public Property Sort As Boolean
        Public Property Descending As Boolean
        Public Property Priority As Integer

        Public Shared PriorityComparer As IComparer(Of SortInfo) = New PriorityComparerImpl()

        Private Class PriorityComparerImpl
            Implements IComparer(Of SortInfo)

            Private Function Compare(x As SortInfo, y As SortInfo) As Integer Implements IComparer(Of SortInfo).Compare
                If x.Priority > y.Priority Then Return 1
                If x.Priority < y.Priority Then Return -1
                Return 0
            End Function
        End Class
    End Class

    Private Sub UpButton_Click(sender As Object, e As Windows.RoutedEventArgs)
        Dim sinfo As SortInfo = sender.DataContext
        sinfo.Priority = sinfo.Priority - 1
        Dim sinfos As Windows.Data.ListCollectionView = SortLst.ItemsSource
        sinfo.Priority = Math.Max(0, Math.Min(sinfos.Count, sinfo.Priority))
        'sinfos.Sort(SortInfo.PriorityComparer)
        'SortLst.ItemsSource = sinfos
        sinfos.Refresh()
    End Sub

    Private Sub DownButton_Click(sender As Object, e As Windows.RoutedEventArgs)
        Dim sinfo As SortInfo = sender.DataContext
        sinfo.Priority = sinfo.Priority + 1
        Dim sinfos As Windows.Data.ListCollectionView = SortLst.ItemsSource
        sinfo.Priority = Math.Max(0, Math.Min(sinfos.Count, sinfo.Priority))
        'sinfos.Sort(SortInfo.PriorityComparer)
        'SortLst.ItemsSource = sinfos
        sinfos.Refresh()
    End Sub
End Class
